package com.xydtech.busiflw.service.impl;

import com.xydtech.busiflw.domain.BusiflDocList;
import com.xydtech.busiflw.domain.BusiflDocReg;
import com.xydtech.busiflw.mapper.BusiflDocListMapper;
import com.xydtech.busiflw.mapper.BusiflDocRegMapper;
import com.xydtech.busiflw.service.IBusiflDocListService;
import com.xydtech.common.config.XydtechBusiPlatConfig;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * 业务流程影像上传明细Service业务层处理
 *
 * @author xydtech
 * @date 2022-01-05
 */
@Service
public class BusiflDocListServiceImpl implements IBusiflDocListService {
    @Autowired
    private BusiflDocListMapper busiflDocListMapper;
    @Autowired
    private BusiflDocRegMapper busiflDocRegMapper;

    /**
     * 查询业务流程影像上传明细
     *
     * @param id 业务流程影像上传明细主键
     * @return 业务流程影像上传明细
     */
    @Override
    public BusiflDocList selectBusiflDocListById(Long id) {
        return busiflDocListMapper.selectBusiflDocListById(id);
    }

    /**
     * 查询业务流程影像上传明细列表
     *
     * @param busiflDocList 业务流程影像上传明细
     * @return 业务流程影像上传明细
     */
    @Override
    public List<BusiflDocList> selectBusiflDocListList(BusiflDocList busiflDocList) {
        return busiflDocListMapper.selectBusiflDocListList(busiflDocList);
    }

    /**
     * 查询业务流程影像上传明细列表
     *
     * @param instanceId 业务流程实例号
     * @return 业务流程影像上传明细集合
     */
    @Override
    public List<BusiflDocList> selectBusiflDocListByInstanceId(String instanceId) {
        return busiflDocListMapper.selectBusiflDocListByInstanceId(instanceId);
    }

    /**
     * 新增业务流程影像上传明细
     *
     * @param busiflDocList 业务流程影像上传明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBusiflDocList(BusiflDocList busiflDocList) {
        BusiflDocReg busiflDocReg = new BusiflDocReg();
        busiflDocReg.setDocRegId(busiflDocList.getDocRegId());
        busiflDocReg.setDocNums(1L);
        busiflDocRegMapper.updateBusiflDocReg(busiflDocReg);
        busiflDocList.setCreateTime(DateUtils.getNowDate());
        return busiflDocListMapper.insertBusiflDocList(busiflDocList);
    }

    /**
     * 修改业务流程影像上传明细
     *
     * @param busiflDocList 业务流程影像上传明细
     * @return 结果
     */
    @Override
    public int updateBusiflDocList(BusiflDocList busiflDocList) {
        return busiflDocListMapper.updateBusiflDocList(busiflDocList);
    }

    /**
     * 批量删除业务流程影像上传明细
     *
     * @param ids 需要删除的业务流程影像上传明细主键
     * @return 结果
     */
    @Override
    public int deleteBusiflDocListByIds(String ids) {
        return busiflDocListMapper.deleteBusiflDocListByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除业务流程影像上传明细信息
     *
     * @param id 业务流程影像上传明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBusiflDocListById(Long id, Long docRegId) {
        BusiflDocReg busiflDocReg = new BusiflDocReg();
        busiflDocReg.setDocRegId(docRegId);
        busiflDocReg.setDocNums(-1L);
        //更新文件数
        busiflDocRegMapper.updateBusiflDocReg(busiflDocReg);
        //查询文件名
        BusiflDocList busiflDocList = busiflDocListMapper.selectBusiflDocListById(id);
        String fileName = busiflDocList.getFileName();
        /** 删除目录下文件*/
        File file = new File(fileName.replace("/profile", XydtechBusiPlatConfig.getProfile()));
        if (file.exists()) {
            file.delete();
        }
        return busiflDocListMapper.deleteBusiflDocListById(id);
    }
}
